module.exports = function ($scope, $resource, $modal, $stateParams, toaster) {
  $scope.type = $stateParams.type;
  console.log($scope.type);
  $scope.para1 = {};
  if ($scope.type == 'read') {
    $scope.para1 = {
        'category_state' : 1
      }
  }
  /* 分页
    * ========================================= */
    $scope.maxSize = 5;             //最多显示多少个按钮
    $scope.bigCurrentPage = 1;      //当前页码
    $scope.itemsPerPage = 10         //每页显示几条
    $scope.searchform = {};
	//查询法规列表
	$scope.search = function(pageNo) {
		var para = {
			pageNo:$scope.bigCurrentPage, 
      pageSize:$scope.itemsPerPage,
    }
    para = angular.extend(para, $scope.para1, $scope.searchform);
		$resource('/api/ac/goc/viewAlevelIndexCategoryService/findViewAlevelIndexList', {}, {}).save(para, function(res){
				if (res.errcode === 0) {
					$scope.categoryList = res.data.results;
					console.log($scope.categoryList);
					$scope.totalItems = res.data.totalRecord;
					$scope.currentPage = pageNo;
				} else {
						toaster.error({title: '', body: '查询信息失败'});
				}
		});
	}

	//加载参加招徕奖励活动的机构名单
	$scope.loadCompany = function() {
		$resource('', {}, {}).save({}, function(res){
            if (res.errcode === 0) {
				$scope.searchform.company_list = res.data;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	}

	init();
	function init() {
		$scope.currentPage = 1;			//当前页码
		$scope.itemsPerPage = 10;		//每页显示几条
		$scope.searchform = {
			'issue_status': '',
			'status': [
				{'name': '全部', 'code': ''},
				{'name': '已发放', 'code': '1'},
				{'name': '未发放', 'code': '0'}
			]
		};
		$scope.search(1);
		$scope.loadCompany();
	}

	//详情&确认发放
	$scope.add = function() {
		$scope.item = {};
		$scope.flag = 'create';
		var modalInstance = $modal.open({
			template: require('../views/addCategory.html'),
			controller: 'addCategory',
			size: 'lg',
			resolve: {
					item: function () {
							return $scope.item;
					},
					flag: function () {
							return $scope.flag;
          },
          type: function () {
              return $scope.type;
          }
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			$scope.search($scope.currentPage);
		});
	}
	$scope.getInfo = function(item) {
		$scope.item = item;
		$scope.flag = 'info';
		var modalInstance = $modal.open({
			template: require('../views/addCategory.html'),
			controller: 'addCategory',
			size: 'lg',
			resolve: {
					item: function () {
							return $scope.item;
					},
					flag: function () {
							return $scope.flag;
          },
          type: function () {
              return $scope.type;
          },
			}
		});


		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			$scope.search($scope.currentPage);
		});
	}
    // 发布
  $scope.public = function(category_id){
    if (confirm('确定要发布吗?')) {
      $resource('/api/as/goc/viewAlevelIndexCategoryAd/updateAlevelIndexCategoryOn', {}, {}).
      save({'category_id': category_id},function(res) {
          if (res.errcode !== 0) {
              toaster.error({title:"",body:res.errmsg});
              return;
          }
          console.log(res);
          toaster.success({title:"",body:"发布成功"});
          $scope.search();
      })
      return;
    } 
  };  
// 撤回
  $scope.withdraw = function(category_id){
    if (confirm('确定要撤回吗?')) {
      $resource('/api/as/goc/viewAlevelIndexCategoryAd/updateAlevelIndexCategoryOff', {}, {}).
      save({'category_id': category_id},function(res) {
          if (res.errcode !== 0) {
              toaster.error({title:"",body:res.errmsg});
              return;
          }
          console.log(res);
          toaster.success({title:"",body:"撤回成功"});
          $scope.search();
      })
      return;
    } 
  }
  
    // 删除
    $scope.delete = function(category_id) {
        console.log(category_id);
        if (confirm('确定要删除吗?')) {
            $resource('/api/as/goc/viewAlevelIndexCategoryAd/DelAlevelIndexCategory', {}, {}).            
            save({'category_id':category_id},function(res) {
                if (res.errcode !== 0) {
                    toaster.error({title:"",body:res.errmsg});
                    return;
                } 
                toaster.success({title:"",body:"删除成功"});                
                $scope.search();
            })
            return;
        } 
        
    };
    // 申请
    // $scope.delete = function(category_id) {
    //     if (confirm('确定要删除吗?')) {
    //         $resource('/api/as/goc/viewAlevelIndexCategoryAd/DelAlevelIndexCategory', {}, {}).            
    //         save({'category_id':category_id},function(res) {
    //             if (res.errcode !== 0) {
    //                 toaster.error({title:"",body:res.errmsg});
    //                 return;
    //             } 
    //             toaster.success({title:"",body:"删除成功"});                
    //             $scope.search();
    //         })
    //         return;
    //     } 
    // };

};